import {
	Component,
	OnInit
} from '@angular/core';
import {Random} from 'random';
import {Option} from '../model/option';

@Component({
	selector: 'app-tarot',
	templateUrl: './tarot.component.html',
	styleUrls: ['./tarot.component.css']
})
export class TarotComponent implements OnInit {

	public step1: boolean | undefined;
	public step2: boolean | undefined;
	public step3: boolean | undefined;
	public selectedOption: number;
	public options: Option[];
	public cards: number[];

	constructor() {
		this.selectedOption = 3;
		this.options = [
			new Option(1, '一'),
			new Option(3, '三'),
			new Option(4, '四'),
			new Option(5, '五'),
			new Option(7, '七'),
			new Option(9, '九')
		];
		this.cards = [];
	}

	ngOnInit(): void {
		this.randomize();
	}

	hasElement(integer: number): boolean {
		for (let element of this.cards) {
			if (element === integer) {
				return true;
			}
		}

		return false;
	}

	randomize() {
		this.toggleStep1();

		this.cards = [];
		for (let i = 0; i < this.selectedOption; ++i) {
			this.cards.push(
				this.randomInteger()
			);
		}
	}

	randomInteger(): number {
		let random = new Random();

		let integer = random.int(0, 47);
		if (this.cards.length === 0) {
			return integer;
		}

		while (this.hasElement(integer)) {
			integer = random.int(0, 47);
		}

		return integer;
	}

	toggleStep1() {
		this.step1 = true;
		this.step2 = false;
		this.step3 = false;
	}

	toggleStep2() {
		this.step1 = false;
		this.step2 = true;
		this.step3 = false;
	}

	toggleStep3() {
		this.randomize();

		this.step1 = false;
		this.step2 = false;
		this.step3 = true;
	}
}